﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace WebMonitorServiceInstaller
{
    public partial class Form1 : Form
    {
        private string _servicePath;
        private string _configFilePath;

        public Form1()
        {
            InitializeComponent();
            LoadConfiguration();
        }

        private void LoadConfiguration()
        {
            // 从配置文件中读取 ServicePath
            _servicePath = ConfigurationManager.AppSettings["ServicePath"];
            _configFilePath = _servicePath + ".config"; // 配置文件的路径
        }

        private void btnInstall_Click(object sender, EventArgs e)
        {
            InstallService();
        }

        private void btnUninstall_Click(object sender, EventArgs e)
        {
            UninstallService();
        }

        private void InstallService()
        {
            try
            {
                // 检查服务可执行文件和配置文件是否存在
                if (!File.Exists(_servicePath) || !File.Exists(_configFilePath))
                {
                    MessageBox.Show("Service executable or config file not found.");
                    return;
                }

                // 安装服务
                Process process = new Process();
                process.StartInfo.FileName = "sc.exe";
                process.StartInfo.Arguments = $"create WebMonitorService binPath= \"{_servicePath}\" start= auto"; // 设置为自动启动
                process.StartInfo.Verb = "runas"; // 以管理员身份运行
                process.Start();
                process.WaitForExit();

                // 检查服务是否安装成功
                if (process.ExitCode == 0)
                {
                    // 启动服务
                    Process startProcess = new Process();
                    startProcess.StartInfo.FileName = "sc.exe";
                    startProcess.StartInfo.Arguments = "start WebMonitorService";
                    startProcess.StartInfo.Verb = "runas"; // 以管理员身份运行
                    startProcess.Start();
                    startProcess.WaitForExit();

                    if (startProcess.ExitCode == 0)
                    {
                        MessageBox.Show("Service installed and started successfully.");
                    }
                    else
                    {
                        MessageBox.Show("Service installed but failed to start.");
                    }
                }
                else
                {
                    MessageBox.Show("Failed to install service.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error installing service: {ex.Message}");
            }
        }

        private void UninstallService()
        {
            try
            {
                // 停止服务
                Process stopProcess = new Process();
                stopProcess.StartInfo.FileName = "sc.exe";
                stopProcess.StartInfo.Arguments = "stop WebMonitorService";
                stopProcess.StartInfo.Verb = "runas"; // 以管理员身份运行
                stopProcess.Start();
                stopProcess.WaitForExit();

                // 卸载服务
                Process process = new Process();
                process.StartInfo.FileName = "sc.exe";
                process.StartInfo.Arguments = "delete WebMonitorService";
                process.StartInfo.Verb = "runas"; // 以管理员身份运行
                process.Start();
                process.WaitForExit();

                MessageBox.Show("Service uninstalled successfully.");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error uninstalling service: {ex.Message}");
            }
        }
    }
}